//
//  QKDatePicker.swift
//  HHSwift
//
//  Created by 华宏 on 2019/10/5.
//  Copyright © 2019 huahong. All rights reserved.
//

import UIKit
protocol QKDatePickerDelegate {
    func pickerView(_ pickerView: QKDatePicker, didSelectDate selectDate: Date, selectText text: String)
}
class QKDatePicker: UIView {

     var delegate:QKDatePickerDelegate?
     fileprivate  var defaultDate:Date!
     fileprivate  var datePickerModel:UIDatePicker.Mode!
    
    /// 输出日期格式
    var dateFormat:String = "yyyy-MM-dd"
    
    var minimumDate:Date? {
        willSet{
            self.datePicker.minimumDate = newValue
        }
    }
    
     var maximumDate:Date? {
           willSet{
               self.datePicker.maximumDate = newValue
           }
       }
    
       
       typealias SureBtnBlock = (_ selectDate:Date,_ selectedText:String)->Void
       
           //声明闭包
           var sureBtnBlock:SureBtnBlock?
        
//    convenience init(defaultDate:Date = Date(),datePickerModel:UIDatePicker.Mode = .date) {
//
//        self.init(frame: UIScreen.main.bounds)
//
//        self.defaultDate = defaultDate
//        self.datePickerModel = datePickerModel
//
//        self.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0, alpha: 0.3)
//
//        self.addSubview(self.datePicker)
//
//        self.addSubview(self.toolBar)
//
//
//    }
//
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(defaultDate:Date = Date(),datePickerModel:UIDatePicker.Mode = .date) {

          super.init(frame: UIScreen.main.bounds)

          self.defaultDate = defaultDate
          self.datePickerModel = datePickerModel

        self.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0, alpha: 0.3)

          self.addSubview(self.datePicker)

          self.addSubview(self.toolBar)

          }
    
    
   func show()  {
       
       UIApplication.shared.keyWindow?.addSubview(self)
   }
       
//MARK: - lazy
   
   lazy var toolBar: UIToolbar = {
       let toolBar = UIToolbar.init(frame: CGRect(x: 0, y: UIScreen.main.bounds.size.height - self.datePicker.bounds.size.height - 40, width: UIScreen.main.bounds.size.width, height: 40))
       toolBar.backgroundColor = UIColor.white
       let leftItem = UIBarButtonItem.init(title: "  取消", style: .plain, target: self, action: #selector(cancelBtnClicked))
       let centerItem = UIBarButtonItem.init(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
       let rightItem = UIBarButtonItem(title: "确定  ", style: .plain, target: self, action: #selector(sureBtnClicked))
       toolBar.items = [leftItem,centerItem,rightItem]
       return toolBar
   }()
   
    
   lazy var datePicker: UIDatePicker = {

    let datePicker = UIDatePicker()
    datePicker.backgroundColor = .white
    datePicker.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - 216, width: UIScreen.main.bounds.size.width, height: 216)
    datePicker.locale = Locale(identifier: "zh_CN")
    datePicker.datePickerMode = self.datePickerModel
    datePicker.date = self.defaultDate
    
       
       return datePicker
   }()
       
//MARK: - Action:
   @objc func cancelBtnClicked() {
       UIApplication.shared.keyWindow?.subviews.last?.removeFromSuperview()
   }
   
   @objc func sureBtnClicked() {

    
    let formatter = DateFormatter()
        formatter.dateFormat = self.dateFormat
        formatter.locale = Locale(identifier: "zh_CN")
    let dateString =  formatter.string(from: datePicker.date)
    
       sureBtnBlock?(datePicker.date,dateString)
     
      delegate?.pickerView(self, didSelectDate: datePicker.date, selectText: dateString)
       
       UIApplication.shared.keyWindow?.subviews.last?.removeFromSuperview()
      
   }
       
       
       
}

       
